import axios from 'axios'
import {
    Notify
} from 'vant'

import {
    getToken
} from './auth'

const instance = new axios.create({
    baseURL: "http://123.56.10.152:3000",
    timeout: 5000,
})
// 请求拦截
instance.interceptors.request.use(
    function (config) {

        return config;
    },
    function (error) {
        return Promise.reject(error);
    }
);

// 响应拦截
instance.interceptors.response.use(
    function (response) {
        return response.data;
    },
    function (error) {
        const {
            status
        } = error.response;
        console.log(status);
        if (status === 401) {
            Notify({
                type: "warning",
                message: "未登录，请先登录"
            });
            window.location.href = "#/login";
        }
        return Promise.reject(error);
    }
);


export const get = (url, params) => instance.get(url, {
    params: {
        ...params,
        cookie: getToken('cookie')
    },
    withCredentials: true,

});


//  导出
export default instance;